﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using AVCare.LMS.Util;
using System.Security.Cryptography;

namespace AVCare.LMS.IO
{
    public class FileOption
    {
        public static string GetUserPath(string userID)
        {
            return Path.Combine(SystemConfigurationManager.FileRoot, userID);
        }

        public static byte[] GetFile(string file)
        {
            if (!File.Exists(file))
            {
                throw new FileNotFoundException();
            }
            using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                int length = (int)fs.Length;
                byte[] data = new byte[length];
                fs.Read(data, 0, length);
                return data;
            }
        }

        public static string GetFileMD5(string file)
        {
            if (!File.Exists(file))
            {
                throw new FileNotFoundException();
            }
            MD5 md5 = MD5.Create();
            StringBuilder sb = new StringBuilder();

            using (FileStream fs = File.Open(file, FileMode.Open))
            {
                foreach (byte b in md5.ComputeHash(fs))
                    sb.Append(b.ToString("x2").ToLower());
            }
            return sb.ToString();
        }

        public static long GetFileSize(string file)
        {
            if (!File.Exists(file))
            {
                throw new FileNotFoundException();
            }
            FileInfo info = new FileInfo(file);
            return info.Length;
        }

        public static void CreateFolder(string path)
        {
            if (Directory.Exists(path))
            {
                return;
            }

            Directory.CreateDirectory(path);
        }

        public static void DeleteFolder(string path)
        {
            if (Directory.Exists(path))
            {
                Directory.Delete(path, true);
            }
        }

        public static void DeleteFile(string path)
        {
            if (File.Exists(path))
            {
                File.Delete(path);
            }
        }
    }
}
